package com.videoaggre.zcl.utils

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.videoaggre.zcl.App

object SPUtils {
    private const val SP_NAME = "spName"
    private val instance: SharedPreferences
        get() = App.app.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)

    fun contains(key: String) = instance.contains(key)
    fun remove(key: String, commit: Boolean = false) = instance.edit(commit) { remove(key) }

    fun put(key: String, value: String, commit: Boolean = false) = instance.edit(commit) { putString(key, value) }
    fun put(key: String, value: Int, commit: Boolean = false) = instance.edit(commit) { putInt(key, value) }
    fun put(key: String, value: Boolean, commit: Boolean = false) = instance.edit(commit) { putBoolean(key, value) }
    fun put(key: String, value: Float, commit: Boolean = false) = instance.edit(commit) { putFloat(key, value) }
    fun put(key: String, value: Long, commit: Boolean = false) = instance.edit(commit) { putLong(key, value) }
    fun put(key: String, value: Set<String>, commit: Boolean = false) = instance.edit(commit) { putStringSet(key, value) }

    fun getString(key: String, defValue: String = "") = instance.getString(key, defValue) ?: ""
    fun getInt(key: String, defValue: Int = -1) = instance.getInt(key, defValue)
    fun getBoolean(key: String, defValue: Boolean = false) = instance.getBoolean(key, defValue)
    fun getFloat(key: String, defValue: Float = -1f) = instance.getFloat(key, defValue)
    fun getLong(key: String, defValue: Long = -1L) = instance.getLong(key, defValue)
    fun getStringSet(key: String, defValue: Set<String> = mutableSetOf()) = instance.getStringSet(key, defValue) ?: mutableSetOf()
}